#include <stdio.h>
#include <string.h>
#include <stdlib.h>




#include"MyString.h"
#include"TelephoneInfo.h"

#define MAX_NAME_LENGTH 50
#define MAX_PHONE_LENGTH 15

// 定义联系人结构体
struct Contact {
    char name[MAX_NAME_LENGTH];
    char phone[MAX_PHONE_LENGTH];
};

// 动态分配内存的联系人数组和当前联系人数
struct Contact *contacts = NULL;
int numContacts = 0;

// 添加联系人函数
void addContact() {
    struct Contact newContact;

    printf("请输入联系人姓名：");
    scanf("%s", newContact.name);

    // 检查是否存在重复联系人
    for (int i = 0; i < numContacts; i++) {
        if (strcmp(contacts[i].name, newContact.name) == 0) {
            printf("该联系人已存在于通讯录中\n\n");
            return;
        }
    }

    printf("请输入联系人电话号码：");//
    scanf("%s", newContact.phone);

    // 分配新的内存空间来存储联系人信息
    contacts = (struct Contact *)realloc(contacts, (numContacts + 1) * sizeof(struct Contact));
    contacts[numContacts++] = newContact;

    printf("联系人已添加\n\n");
}

// 显示所有联系人函数
void displayContacts() {
    if (numContacts == 0) {
        printf("通讯录为空\n\n");
    } else {
        printf("所有联系人信息：\n");
        for (int i = 0; i < numContacts; i++) {
            printf("姓名：%s， 电话号码：%s\n", contacts[i].name, contacts[i].phone);
        }
        printf("\n");
    }
}

// 删除联系人函数
void deleteContact() {
    if (numContacts == 0) {
        printf("通讯录为空，无法删除联系人\n\n");
        return;
    }

    char nameToDelete[MAX_NAME_LENGTH];
    printf("请输入要删除的联系人姓名：");
    scanf("%s", nameToDelete);

    int found = 0;
    for (int i = 0; i < numContacts; i++) {
        if (strcmp(contacts[i].name, nameToDelete) == 0) {
            found = 1;
            for (int j = i; j < numContacts - 1; j++) {
                contacts[j] = contacts[j + 1];
            }
            numContacts--;
            contacts = (struct Contact *)realloc(contacts, numContacts * sizeof(struct Contact));
            printf("联系人已删除\n\n");
            break;
        }
    }

    if (!found) {
        printf("未找到该联系人\n\n");
    }
}

// 释放动态分配的内存
void freeContacts() {
    free(contacts);
}

int main1() {
    int choice;

    do {
        printf("通讯录应用程序\n");
        printf("1. 添加联系人\n");
        printf("2. 显示所有联系人\n");
        printf("3. 删除联系人\n");
        printf("4. 退出\n");
        printf("请选择操作：");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addContact();
                break;
            case 2:
                displayContacts();
                break;
            case 3:
                deleteContact();
                break;
            case 4:
                printf("感谢使用，再见！\n");
                break;
            default:
                printf("无效的选项，请重新选择\n\n");
        }
    } while (choice != 4);

    // 释放动态分配的内存
    freeContacts();

    return 0;
}
bool BigerID(void *p1,void *p2)
{
    Stu *s1=(Stu*)p1;
    Stu *s2=(Stu*)p2;
    return s1->s2;>s2->id;
}
int main()
{
    DArray *a=InitDArray();
    InsertDArray(a,CresteStu(3,"zhangsan"));
    InsertDArray(a,CresteStu(3,"zhangsan"));
    InsertDArray(a,CresteStu(3,"zhangsan"));

    FAstSort(A,SmallerID);

    PrintDArray(a,PrinStu);
    return 0;


}
bool BiggerName(void *p1,void *p2)
{
    Stu *s1=(Stu *)p1;
    Stu *s2=(Stu *)p2;
    return strcmp(s1->name,s2->name)<0;
}




